<?php
require_once __dir__.'/../common.php';

$item_x = $_POST['x'];
$item_y = $_POST['y'];
$item_x = $_REQUEST['x'];
$item_y = $_REQUEST['y'];

$result = sys::sql("select * from items where item_x=%d && item_y=%d;",
	$item_x, $item_y);

if (empty($result) || empty($result[0]))
{
	$valid_result = get_map_valid_xy($item_x, $item_y);

	if ($valid_result == 0)
	{
		$message = "有效陆地，可以创建。";
	}
	else if ($valid_result == -1)
	{
		$message = "那里超出地图边界，无法创建。";
	}
	else if ($valid_result == -2)
	{
		$message = "目前只能在白色的空地上创建。";
	}
	else if ($valid_result == -3)
	{
		$message = "离自己已有内容太远了，请靠近自己的一些（最远".MAXIMUM_DISTANCE_FROM_USER_EXISTING_OBJECT."格）。";
	}
	else if ($valid_result == -4)
	{
		$message = "离别人的内容太近了，请保持间距（最近".MINIMUM_DISTANCE_FROM_OTHER_USER_EXISTING_OBJECT."格）。";
	}
	
	$ret_array = array("result"=>$valid_result, "message"=>$message);
	if ($valid_result = -3)
	{
		$result = sys::sql("select * from items where user_id='%s' order by item_id desc limit 1;", user::getId());
		if ($result[0])
		{
			$ret_array['last_item_x'] = $result[0]['item_x'];
			$ret_array['last_item_y'] = $result[0]['item_y'];
		}
	}

	echo json_encode($ret_array);
}
else
{
	$item_info = item::appendImageRealUrl($result[0]);
	$item_info['result'] = true;
	$item_info['item_image'] = $item_info['item_image_l'];
	$image_file = $_SERVER['DOCUMENT_ROOT'].$item_info['item_image_l'];
	$image_info = getimagesize($image_file);
	$item_info['item_image_width'] = $image_info[0];
	$item_info['item_image_height'] = $image_info[1];
	//echo json_encode(array_merge(array("result"=>true), $item_info));
	echo json_encode($item_info);
}

function get_user_valid_xy($x, $y)
{
	$user_id = user::getId();
	if (empty($user_id)) return false;

	$result = sys::sql("select count(*) as count from items where user_id='%s';", $user_id);
	$user_item_count = $result[0]['count'];
	
	// 如果用户已经有元素就需要和已有元素进行对比——不能太远
/*	if ($user_item_count)
	{
		$sql = sys::getSqlString("select count(*) as count from items "
			."where (user_id=%s and item_x!=NULL and abs(item_x-%d)<=%d and abs(item_y-%d)<=%d);",
			$user_id,
			$x, MAXIMUM_DISTANCE_FROM_USER_EXISTING_OBJECT,
			$y, MAXIMUM_DISTANCE_FROM_USER_EXISTING_OBJECT);
		$result = sys::sql($sql);
		
		//只要找到一个接近的元素就算通过，没有接近元素只能算挂鸟
		if (intval($result[0]['count']) == 0)
			return -3;
	}*/
	
	// 如果上面都通过了，现在和别人的元素对比——不能太近
	$sql = sys::getSqlString("select count(*) as count from items "
		."where (user_id!=%s and item_x!='' and (abs(item_x-%d)<%d and abs(item_y-%d)<%d)) limit 1;",
		$user_id,
		$x, MINIMUM_DISTANCE_FROM_OTHER_USER_EXISTING_OBJECT,
		$y, MINIMUM_DISTANCE_FROM_OTHER_USER_EXISTING_OBJECT);
	$result = sys::sql($sql);

	//只要找到一个接近的元素就算挂鸟
	if ($result[0]['count'])
		return -4;
	
	return 0;
}

/**
	回值描述
		0 当前用户在$x,$y位置可以放置内容
		-1 出界
		-2 不可以建造的区域
		-3 离自己已有内容太远
		-3 离别人的内容太近
 */
function get_map_valid_xy($x, $y)
{
	$image = imagecreatefromgif(PLAYGROUND_MAP_IMAGE_FILE);
	$rgb = imagecolorsforindex($image,
		imagecolorat($image, $x, $y));
	
	// out of border	
	if ($x<0 || $y<0 || $x>=imagesx($image) || $y>=imagesy($image))
	{
		return -1;
	}
	else
	{
		// can not build - sea or not land
		if (array_sum($rgb) != 255*3)
		{
			return -2;
		}
		// land - can build
		else
		{
			$user_valid = get_user_valid_xy($x, $y);
			if ($user_valid == 0)
			{
				return 0;
			}
			else
			{
				return $user_valid;
			}
		}
	}
}